package com.zimmur.app.mocolink.manage.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.form.AdminForm;
import com.zimmur.app.mocolink.domain.model.Admin;
import com.zimmur.app.mocolink.domain.param.AdminParam;
import com.zimmur.app.mocolink.domain.param.AppUserParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.AdminInfo;
import com.zimmur.app.mocolink.domain.vo.AppUserInfoForManage;
import com.zimmur.app.mocolink.domain.vo.RoleInfo;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IAdminService;
import com.zimmur.app.mocolink.service.IRoleService;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

	@Autowired
	private IAdminService adminService;
	@Autowired
	private IRoleService roleService;
	
	@RequestMapping("/index")
	public String index(ModelMap model){
		AdminParam param = new AdminParam(1);
		PageUtil<AdminInfo> page = adminService.selectByParam(param);
		model.addAttribute("page",page);
		model.addAttribute("param",param);
		return "/center/admin/list";
	}
	@RequestMapping("/query")
	public String query(ModelMap model){
		Integer pageNo = getParam("pageNo", 1);
		String keyword = getParam("keyword");
		AdminParam param = new AdminParam(pageNo,keyword);
		PageUtil<AdminInfo> page = adminService.selectByParam(param);
		model.addAttribute("page",page);
		model.addAttribute("param",param);
		return "/center/admin/list";
	}
	@RequestMapping("/add")
	public String add(ModelMap model){
		List<RoleInfo> roleInfos = roleService.selectAllRoleWithAdminId(0);
		
		model.addAttribute("admin", new Admin());
		model.addAttribute("roleList", roleInfos);
		return "/center/admin/edit";
	}
	@RequestMapping("/edit")
	public String edit(ModelMap model){
		Integer adminId = getParam("adminId", 0);
		Admin admin = adminService.selectById(adminId);
		List<RoleInfo> roleInfos = roleService.selectAllRoleWithAdminId(adminId);
		
		model.addAttribute("admin", admin);
		model.addAttribute("roleList", roleInfos);
		return "/center/admin/edit";
	}
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(AdminForm form){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String username = getUsername();
		form.setAdminUser(username);
		int flag =adminService.saveAdmin(form);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE700401;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer adminId = getParam("adminId", 0);
		int flag =adminService.deleteAdmin(adminId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/updateStatus")
	public ReturnJsonEntity updateStatus(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer adminId = getParam("adminId", 0);
		Integer status = getParam("status", 0);
		String username = getUsername();
		int flag =adminService.updateStatus(adminId, status, username);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	
}
